package org.jivesoftware.smackx;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.WeakHashMap;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ChatManagerListener;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.PacketInterceptor;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.NotFilter;
import org.jivesoftware.smack.filter.PacketExtensionFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.util.collections.ReferenceMap;
import org.jivesoftware.smackx.packet.ChatStateExtension;

public class ChatStateManager
{
  private static final PacketFilter filter;
  private static final Map<Connection, ChatStateManager> managers = new WeakHashMap();
  private final Map<Chat, ChatState> chatStates;
  private final Connection connection;
  private final IncomingMessageInterceptor incomingInterceptor;
  private final OutgoingMessageInterceptor outgoingInterceptor;

  static
  {
    filter = new NotFilter(new PacketExtensionFilter("http://jabber.org/protocol/chatstates"));
  }

  private ChatStateManager(Connection paramConnection)
  {
    this.outgoingInterceptor = new OutgoingMessageInterceptor(this, null);
    this.incomingInterceptor = new IncomingMessageInterceptor(this, null);
    this.chatStates = new ReferenceMap(2, 0);
    this.connection = paramConnection;
  }

  private void fireNewChatState(Chat paramChat, ChatState paramChatState)
  {
    Iterator localIterator = paramChat.getListeners().iterator();
    while (true)
    {
      MessageListener localMessageListener;
      do
      {
        if (!(localIterator.hasNext()))
          return;
        localMessageListener = (MessageListener)localIterator.next();
      }
      while (!(localMessageListener instanceof ChatStateListener));
      ((ChatStateListener)localMessageListener).stateChanged(paramChat, paramChatState);
    }
  }

  // ERROR //
  public static ChatStateManager getInstance(Connection paramConnection)
  {
    // Byte code:
    //   0: aload_0
    //   1: ifnonnull +9 -> 10
    //   4: aconst_null
    //   5: astore 5
    //   7: aload 5
    //   9: areturn
    //   10: getstatic 32	org/jivesoftware/smackx/ChatStateManager:managers	Ljava/util/Map;
    //   13: astore_1
    //   14: aload_1
    //   15: monitorenter
    //   16: getstatic 32	org/jivesoftware/smackx/ChatStateManager:managers	Ljava/util/Map;
    //   19: aload_0
    //   20: invokeinterface 118 2 0
    //   25: checkcast 2	org/jivesoftware/smackx/ChatStateManager
    //   28: astore_3
    //   29: aload_3
    //   30: ifnonnull +27 -> 57
    //   33: new 2	org/jivesoftware/smackx/ChatStateManager
    //   36: dup
    //   37: aload_0
    //   38: invokespecial 120	org/jivesoftware/smackx/ChatStateManager:<init>	(Lorg/jivesoftware/smack/Connection;)V
    //   41: astore_3
    //   42: aload_3
    //   43: invokespecial 123	org/jivesoftware/smackx/ChatStateManager:init	()V
    //   46: getstatic 32	org/jivesoftware/smackx/ChatStateManager:managers	Ljava/util/Map;
    //   49: aload_0
    //   50: aload_3
    //   51: invokeinterface 127 3 0
    //   56: pop
    //   57: aload_1
    //   58: monitorexit
    //   59: aload_3
    //   60: astore 5
    //   62: goto -55 -> 7
    //   65: astore_2
    //   66: aload_1
    //   67: monitorexit
    //   68: aload_2
    //   69: athrow
    //
    // Exception table:
    //   from	to	target	type
    //   16	68	65	finally
  }

  private void init()
  {
    this.connection.getChatManager().addOutgoingMessageInterceptor(this.outgoingInterceptor, filter);
    this.connection.getChatManager().addChatListener(this.incomingInterceptor);
    ServiceDiscoveryManager.getInstanceFor(this.connection).addFeature("http://jabber.org/protocol/chatstates");
  }

  private boolean updateChatState(Chat paramChat, ChatState paramChatState)
  {
    int i;
    if ((ChatState)this.chatStates.get(paramChat) != paramChatState)
    {
      this.chatStates.put(paramChat, paramChatState);
      i = 1;
    }
    while (true)
    {
      return i;
      i = 0;
    }
  }

  public boolean equals(Object paramObject)
  {
    boolean bool;
    if (this == paramObject)
      bool = true;
    while (true)
    {
      while (true)
      {
        return bool;
        if ((paramObject != null) && (super.getClass() == paramObject.getClass()))
          break;
        bool = false;
      }
      ChatStateManager localChatStateManager = (ChatStateManager)paramObject;
      bool = this.connection.equals(localChatStateManager.connection);
    }
  }

  public int hashCode()
  {
    return this.connection.hashCode();
  }

  public void setCurrentState(ChatState paramChatState, Chat paramChat)
    throws XMPPException
  {
    if ((paramChat == null) || (paramChatState == null))
      throw new IllegalArgumentException("Arguments cannot be null.");
    if (!(updateChatState(paramChat, paramChatState)));
    while (true)
    {
      return;
      Message localMessage = new Message();
      localMessage.addExtension(new ChatStateExtension(paramChatState));
      paramChat.sendMessage(localMessage);
    }
  }

  private class IncomingMessageInterceptor
  implements ChatManagerListener, MessageListener
  {
    public void chatCreated(, boolean paramBoolean)
    {
      paramChat.addMessageListener(this);
    }

    public void processMessage(, Message paramMessage)
    {
      PacketExtension localPacketExtension = paramMessage.getExtension("http://jabber.org/protocol/chatstates");
      if (localPacketExtension == null)
        return;
      try
      {
        ChatState localChatState = ChatState.valueOf(localPacketExtension.getElementName());
        ChatStateManager.access$2(this.this$0, paramChat, localChatState);
      }
      catch (Exception localException)
      {
      }
    }
  }

  private class OutgoingMessageInterceptor
  implements PacketInterceptor
  {
    public void interceptPacket()
    {
      Message localMessage = (Message)paramPacket;
      Chat localChat = ChatStateManager.access$0(this.this$0).getChatManager().getThreadChat(localMessage.getThread());
      if (localChat == null);
      while (true)
      {
        do
          return;
        while (!(ChatStateManager.access$1(this.this$0, localChat, ChatState.active)));
        localMessage.addExtension(new ChatStateExtension(ChatState.active));
      }
    }
  }
}